limsSampleHandle.auditPage = function (pageType) {

    return {
        getGridParam: function () {
            var _this = this;
            var columns = limsSampleHandle.getAuditListColumns(pageType); // 入库、出库、销毁
            var toolbar = [{
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.PASS',
                icon: 'pass',
                onClick: function () {
                    _this.pass();
                }
            }, {
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.REJECT',
                icon: 'reject',
                onClick: function () {
                    _this.reject();
                }
            }];
            if (pageType === 'back') {
                columns = limsSampleHandle.getBackAuditListColumns();  // 返样
                toolbar.push({
                    type: 'button',
                    text: 'GIKAM.BUTTON.COURIER.EDIT',
                    icon: 'edit',
                    onClick: function () {
                        _this.fillInLogistics();
                    }
                })
            }
            _this.param.createdByOrgId_EQ = workspace.user.ext$.loginorgid;
            return {
                type: 'grid',
                id: _this.idManage.handleGridId,
                url: limsSampleHandle.baseUrl + '/queries/tasks',
                columns: columns,
                requestData: _this.param,
                service: 'limsSampleHandleServiceImpl',
                onLoadSuccess: function (data) {
                    if (!Gikam.isEmpty(data)) {
                        this.activeRowByIndex(0);
                    } else {
/*                        Gikam.cleanCompData([_this.idManage.sampleGridId, _this.idManage.flowGridId]);
                        Gikam.getAsyncComp(_this.idManage.uploaderId).done(function (comp) {
                            comp.setOptions({
                                bizId: -1,
                            });
                        });*/
                    }
                },
                onRowActive: function (index, row) {
/*                    Gikam.getAsyncComp(_this.idManage.sampleGridId).done(function (grid) {
                        grid.refresh({
                            url: limsSampleHandle.sampleUrl + '/queries/sampleHandles',
                            requestData: {
                                sampleHandleId_EQ: row.id
                            }
                        });
                    });
                    Gikam.getAsyncComp(_this.idManage.uploaderId).done(function (comp) {
                        comp.setOptions({
                            bizId: row.id,
                        });
                    });
                    Gikam.getAsyncComp(_this.idManage.flowGridId).done(function (comp) {
                        comp.refresh({
                            url: IFM_CONTEXT + '/secure/core/module/sys/bpmn-comments/queries?table=T_LIMS_SAMPLE_HANDLE&id=' + row.id
                        });
                    });*/
                },
                toolbar: toolbar,
                filterOpen: true,
                page: true,
                onCellClick: function (field, row) {
                    field === 'handleCode' && limsSampleHandle.loadSampleHandleDetailPage(row, pageType, 'audit')
                },
            }
        },

        pass: function () {
            var _this = this;
            var grid = Gikam.getComp(_this.idManage.handleGridId);
            if (pageType === 'enter') {
                var selections = grid.getSelections();
                if (Gikam.isEmpty(selections)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                    return
                }
                var checkLocationsUrl = limsSampleHandle.baseUrl + '/check-samplelocations';
                Gikam.postText(checkLocationsUrl, Gikam.getJsonWrapper(null, [null, selections])).done(function (req) {
                    req = JSON.parse(req);
                    if (!req.message) {
                        Gikam.create('workflow').pass({
                            data: grid.getSelections(),
                            pageObject: limsSampleHandle,
                            formatterToolbar: (toolbar, type, formId) => {
                                toolbar.forEach(b => {
                                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                                        b.class = 'blue'
                                        b.icon = 'save'
                                        b.color = 'white'
                                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                                        b.icon = 'back'
                                    }
                                })
                                return toolbar
                            }
                        }).done(function () {
                            grid.refresh()
                        })
                    } else {
                        Gikam.confirm('GIKAM.BUTTON.CONFIRM', req.message + Gikam.propI18N('GIKAM.TIP.SAMPLE_NOT_SETLOCATIONS.CONFIRM'), function () {
                            Gikam.create('workflow').pass({
                                data: grid.getSelections(),
                                pageObject: limsSampleHandle,
                                formatterToolbar: (toolbar, type, formId) => {
                                    toolbar.forEach(b => {
                                        if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                                            b.class = 'blue'
                                            b.icon = 'save'
                                            b.color = 'white'
                                        } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                                            b.icon = 'back'
                                        }
                                    })
                                    return toolbar
                                }
                            }).done(function () {
                                grid.refresh()
                            })
                        })
                    }
                })
            } else {
                Gikam.create('workflow').pass({
                    data: grid.getSelections(),
                    pageObject: limsSampleHandle,
                    formatterToolbar: (toolbar, type, formId) => {
                        toolbar.forEach(b => {
                            if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                                b.class = 'blue'
                                b.icon = 'save'
                                b.color = 'white'
                            } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                                b.icon = 'back'
                            }
                        })
                        return toolbar
                    }
                }).done(function () {
                    grid.refresh();
                });
            }
        },

        reject: function () {
            var _this = this;
            var grid = Gikam.getComp(_this.idManage.handleGridId);
            Gikam.create('workflow').reject({
                data: grid.getSelections(),
                pageObject: limsSampleHandle,
                formatterToolbar: (toolbar, type, formId) => {
                    toolbar.forEach(b => {
                        if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                            b.class = 'blue'
                            b.icon = 'save'
                            b.color = 'white'
                        } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                            b.icon = 'back'
                        }
                    })
                    return toolbar
                }
            }).done(function () {
                grid.refresh();
            });
        },

        fillInLogistics: function () {
            var _this = this;
            let selections = Gikam.getComp(_this.idManage.handleGridId).getSelections();
            //console.log(_this.idManager.handleGridId);
            if (Gikam.isEmpty(selections)) {
                Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                return;
            }
            Gikam.create('modal', {
                title: 'GIKAM.BUTTON.COURIER.EDIT',
                width: 400,
                height: 200,
                onAfterClose: function (data) {
                    if (Gikam.isNotEmpty(data)) {
                        workspace.window.showMask();
                        var chooseData = selections.map(function (item) {
                            return {
                                id: item.id,
                                courierNumber: data.courierNumber,
                                courierCompany: data.courierCompany
                            }
                        });
                        Gikam.put(limsSampleHandle.baseUrl + '/instant', JSON.stringify({
                            b: ['limsSampleHandleServiceImpl', chooseData]
                        })).done(function (data) {
                            Gikam.getComp(_this.idManage.handleGridId).refresh();
                        }).always(function () {
                            workspace.window.closeMask();
                        });
                    }
                }
            });
            Gikam.create('layout', {
                renderTo: Gikam.getLastModal().window.$dom,
                center: {
                    items: [{
                        type: 'form',
                        id: pageType + 'package-form-obj',//_this.idManage.packFormId
                        columns: 1,
                        titleWidth: 140,
                        fields: [{
                            field: 'courierNumber',
                            title: 'T_LIMS_SAMPLE_HANDLE.COURIERNUMBER',
                            validators: ['notEmpty'],
                        }, {
                            field: 'courierCompany',
                            title: 'T_LIMS_SAMPLE_HANDLE.COURIERCOMPANY',
                            validators: ['notEmpty'],
                            firstBlank: false,
                            type: 'select',
                            category: 'sys',
                            param: {
                                codeCategoryId: 'courierCompany'
                            }
                        }],
                    }, {
                        type: 'btnToolbar',
                        items: [{
                            type: 'button',
                            text: 'GIKAM.BUTTON.CONFIRM',
                            class: 'blue',
                            icon: 'save',
                            color: 'white',
                            onClick: function () {
                                console.log(pageType + 'package-form-obj');
                                //校验判断
                                var form = Gikam.getComp(pageType + 'package-form-obj');
                                if (!form.validate()) {
                                    return;
                                }
                                Gikam.getLastModal().close(form.getData());
                            }
                        }, {
                            type: 'button',
                            text: 'GIKAM.BUTTON.CANCEL',
                            icon: 'back',
                            onClick: function () {
                                Gikam.getLastModal().close();
                            }
                        }]
                    }]
                }
            })
        },

        getSampleHandleAuditInfo: function () {
            var _this = this;
            var toolbar = [];
            var editor = false;
            if (pageType === 'enter') {// 入库
                editor = true;
                toolbar = [{
                    type: 'button',
                    text: 'GIKAM.BUTTON.ASSIGN',
                    icon: 'modify',
                    onClick: function () {
                        var grid = Gikam.getComp(_this.idManage.sampleGridId);
                        var selections = grid.getSelections();
                        if (Gikam.isEmpty(selections)) {
                            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                            return;
                        }
                        let ids = selections.map(function (item) {
                            return item.id
                        }).join(',');
                        Gikam.create('modal', {
                            width: '90%',
                            height: '90%',
                            title: Gikam.choose.getConfig('sample-board-choose').title,
                            url: IFM_CONTEXT + Gikam.choose.getConfig("sample-board-choose").url + Gikam.param({
                                id_IN: ids
                            }),
                            onAfterClose: function (rows) {
                                grid.refresh();
                            }
                        });
                    }
                }];
            }
            toolbar.push({
                type: 'form',
                id: _this.idManage.sampleCodeFormId,
                fields: [{
                    field: 'sampleCode',
                    title: 'T_LIMS_SAMPLE.SAMPLECODE',
                    onChange: function () {
                        _this.scanCodeCheck()
                    }
                }]
            });
            return {
                type: 'tab',
                panels: [{
                    title: 'MODULE.BUSINESS.ORDERTASK.TAB.SAMPLE_INFO',
                    items: [{
                        type: 'grid',
                        id: _this.idManage.sampleGridId,
                        columns: limsSampleHandle.getSampleListColumns(editor),
                        toolbar: toolbar,
                        onCellClick: function (field, row) {
                            field === 'ext$.batchcode' && limsSampleHandle.loadBatchDetailPage(row);
                        },
                        onRendered: function () {
                            if (pageType !== 'enter') {
                                this.hideColumns(['ext$.sampleconcentration_temp', 'ext$.samplevolume_temp', 'ext$.restcount_temp'])
                            }
                        },
                    }]
                }, {
                    title: 'GIKAM.FILE.ATTACHMENT',
                    items: [{
                        type: 'uploader',
                        id: _this.idManage.uploaderId,
                        dbTable: 'T_LIMS_SAMPLE_HANDLE',
                        readonly: true,
                        formatterGridColumns: function (columns) {
                            return Gikam.getFormatterGridColumns(columns);
                        }
                    }]
                }, {
                    title: 'T_CORE_FILE.OPERATE.OPERATION.RECORD',
                    items: [{
                        type: 'grid',
                        id: _this.idManage.flowGridId,
                        columns: limsSampleHandle.getFlowListColumns()
                    }]
                }]
            }
        },

        scanCodeCheck: function () {
            var _this = this;
            let formComp = Gikam.getComp(_this.idManage.sampleCodeFormId);
            var sampleCode = formComp.getData().sampleCode;
            var url = limsSampleHandle.sampleUrl + '/scancodecheck';
            if (sampleCode) {
                var sampleHandleRow = Gikam.getComp(_this.idManage.handleGridId).getActivedRow();
                if (sampleHandleRow) {
                    Gikam.postText(url, Gikam.getJsonWrapper(null, [null, [{
                        sampleHandleId: sampleHandleRow.id,
                        sampleCode: sampleCode
                    }]])).done(function (data) {
                        if (parseInt(data) === 1){
                            Gikam.alert('SAMPLE.SCANCHECK.ALERT.DUPLICATE');
                        }else if (parseInt(data) === 2){
                            Gikam.alert('SAMPLE.SCANCHECK.ALERT.FAIL');
                        }else if (parseInt(data) > 2){
                            Gikam.getComp(_this.idManage.sampleGridId).refreshRowById(parseInt(data));
                        }
                        formComp.cleanData().done(function () {
                            formComp.setData({sampleCode: ''});
                        });
                        formComp.getField('sampleCode').focus();
                    })
                }
            }
        },

        idManage: {
            handleGridId: pageType + 'lims-sample-handle-audit-list-grid',
            sampleGridId: pageType + 'lims-handle-sample-audit-list-grid',
            uploaderId: pageType + 'sample-handle-audit-uploader',
            flowGridId: pageType + 'handle-flow-audit-list-grid',
            packFormId : pageType + 'package-form-obj',
            sampleCodeFormId : pageType + 'sampleCode-form-boj',
        },

        create: function () {
            var _this = this;
            Gikam.create('layout', {
                id: pageType + 'lims-sample-handle-audit-list-layout',
                renderTo: workspace.window.$dom,
                center: {
                    items: [this.getGridParam()]
                }
/*                north: {
                    height: '40%',
                    items: [this.getGridParam()]
                },
                center: {
                    items: [this.getSampleHandleAuditInfo()]
                }*/
            });
        },

        init: function (param) {
            this.param = param;
            this.create();
        }
    }
};
